package com.example.yydm666.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.yydm666.entity.*;
import com.example.yydm666.mapper.CartMapper;
import com.example.yydm666.mapper.OrderDetailMapper;
import com.example.yydm666.mapper.OrdersMapper;
import com.example.yydm666.mapper.UserAddressMapper;
import com.example.yydm666.service.OrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author admin
 * @since 2022-03-28
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private UserAddressMapper userAddressMapper;
    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Override
    public boolean save(Orders orders, User user, String address, String remark) {

//        判断是新地址还是老地址
        if (orders.getUserAddress().equals("newAddress")) {
//            存入数据库
            UserAddress userAddress = new UserAddress();
            userAddress.setAddress(address);
            userAddress.setRemark(remark);
            userAddress.setIsdefault(1);
            userAddress.setUserId(user.getId());
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("isdefault", 1);
            UserAddress userAddress1 = userAddressMapper.selectOne(queryWrapper);
            userAddress1.setIsdefault(0);
            userAddressMapper.updateById(userAddress1);
            userAddressMapper.insert(userAddress);
            orders.setUserAddress(address);
        }
//        存储orders
        orders.setUserId(user.getId());
        orders.setLoginName(user.getLoginName());
        String serialnumber = null;
        try {
            StringBuffer stringBuffer = new StringBuffer();
            for (int i = 0; i < 32; i++) {
                stringBuffer.append(Integer.toHexString(new Random().nextInt(16)));
            }
            serialnumber = stringBuffer.toString().toUpperCase();
        } catch (Exception e) {
            e.printStackTrace();
        }
        orders.setSerialnumber(serialnumber);
        ordersMapper.insert(orders);
//        存储ordersDetail
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",user.getId());
        List<Cart> list = cartMapper.selectList(queryWrapper);
        for (Cart cart : list) {
            OrderDetail orderDetail = new OrderDetail();
            BeanUtils.copyProperties(cart,orderDetail);
            orderDetail.setId(null);
            orderDetail.setOrderId(orders.getId());
            orderDetailMapper.insert(orderDetail);
        }
//        清空购物车
        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("user_id",user.getId());
        cartMapper.delete(queryWrapper1);
        return true;
    }
}
